package com.bear.cmcc.NetTestHTTP.util;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.os.Bundle;

import com.bear.cmcc.NetTestHTTP.component.HttpTestRoundInfoBean;
import com.bear.cmcc.NetTestHTTP.component.NetTestRoundInfoBean;
import com.bear.cmcc.NetTestHTTP.component.NetTestTimesInfoBean;
import com.bear.cmcc.NetTestHTTP.component.ServersConfigBean;
import com.bear.cmcc.NetTestHTTP.constant.FtpConstant;
import com.bear.cmcc.NetTestHTTP.constant.MyCommonConstant;
import com.bear.cmcc.NetTestHTTP.constant.MyCommonConstant.PrivateActionName;
import com.bear.cmcc.NetTestHTTP.db.HttpTestDBControl;
import com.bear.cmcc.NetTestHTTP.db.NetTestDBControl;
import com.bear.cmcc.NetTestHTTP.service.AutoTestService;

/**
 * 
 * http 测试工具类
 * @author zhangheng
 *
 */
public class HttpTestUtils {
    static String TAG = HttpTestUtils.class.getSimpleName();

    /**
     * 启动网络测试服务。
     * 
     * @param context
     * @param timeNum 测试次数，如果是<0那么就无限制。
     */
    public static void startHttpTestService(Context context, int timeNum) {
        // TODO
        Intent intent = new Intent(MyCommonConstant.PrivateActionName.HTTP_TEST_SERVICENAME);
        intent.setClass(context, com.bear.cmcc.NetTestHTTP.service.HttpTestService.class);
        Bundle bundle = new Bundle();
        bundle.putString(PrivateActionName.SERVICE_ACTION,
                PrivateActionName.SERVICE_ACTION_STARTSERVICE);
        bundle.putInt(PrivateActionName.NET_TEST_SERVICE_TESTNUM, timeNum);
        intent.putExtras(bundle);
        context.startService(intent);
    }
    
    /**
     * 停止网络测试服务。
     * 
     * @param context
     */

    public static void stopHttpTestService(Context context, int roundId) {
        // TODO
        sendServiceCommand(MyCommonConstant.PrivateActionName.HTTP_TEST_SERVICENAME,
                PrivateActionName.SERVICE_ACTION_STOPSERVICE, context,
                com.bear.cmcc.NetTestHTTP.service.HttpTestService.class, roundId);
        
    }
    
    /**
     * 通知其他service操作的方法
     * 
     * @param serviceName like
     *            com.funo.networktesttassistant.service.NetTestService
     * @param command 操作命令
     * @param context
     * @param service类
     * @return 网络是否可以使用。
     */
    public static void sendServiceCommand(String serviceName, String command, Context context,
            Class tclass, int roundId) {
        Intent intent = new Intent(serviceName);
        intent.setClass(context, tclass);
        Bundle bundle = new Bundle();
        bundle.putString(PrivateActionName.SERVICE_ACTION, command);
        bundle.putInt(PrivateActionName.SERVICE_ACTION_ROUND_ID, roundId);
        intent.putExtras(bundle);
        context.startService(intent);
    }
    
    /**
     * 插入数据到round表
     * 
     * @param roundInfoBean
     * @param context
     */
    public static void insertRoundInfo(HttpTestRoundInfoBean roundInfoBean, Context context) {
        HttpTestDBControl dbControl = new HttpTestDBControl(context);
        dbControl.insertToTestRoundInfo(roundInfoBean);
    }
    
    /**
     * 更新数据到round表
     * 
     * @param roundInfoBean
     * @param context
     */
    public static void updateOrCreateTestRoundInfo(HttpTestRoundInfoBean roundInfoBean,
            Context context) {
    	HttpTestDBControl dbControl = new HttpTestDBControl(context);
        dbControl.updateOrCreateTestRoundInfo(roundInfoBean);
    }
    
    /**
     * 一轮测试结束发送广播
     * 
     * @param context
     */
    public static void setOneRoundFinishBroad(Context context) {
        Intent intent = new Intent(
                MyCommonConstant.PrivateActionName.HTTP_TEST_ONE_ROUND_FINISH_BROAD);
        context.sendBroadcast(intent);
    }
    
    /**
     * 发送测试过程中的速度广播
     * 
     * @param _averageSpeed 平均速度，应该是轮的平均速度。
     * @param _maxSpeed
     * @param _minSpeed
     * @param context
     */
    public static void sendHttpTestSpeedBroad(double _averageSpeed, double _maxSpeed,
            double _minSpeed, Context context) {
        try {
            Intent intent = new Intent(MyCommonConstant.PrivateActionName.HTTP_TEST_SPEED_BROAD);
            intent.putExtra(MyCommonConstant.PrivateActionName.HTTP_TEST_AVERAGE_SPEED_BROAD,
                    _averageSpeed);
            intent.putExtra(MyCommonConstant.PrivateActionName.HTTP_TEST_MAX_SPEED_BROAD, _maxSpeed);
            intent.putExtra(MyCommonConstant.PrivateActionName.HTTP_TEST_MIN_SPEED_BROAD, _minSpeed);
            context.sendBroadcast(intent);
//            Log.i(TAG, "sendNetTestSpeedBroad ");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    
    public static void setOneRoundBeginBroad(Context context) {
        Intent intent = new Intent(
                MyCommonConstant.PrivateActionName.HTTP_TEST_ONE_ROUND_BEGIN_BROAD);
        context.sendBroadcast(intent);
    }
    
    /**
     * 开启之前关闭过的其他的服务
     * 
     * @param context
     * @param stopedServicesName 之前停止过的servicename集合
     * @param ourServices 传递过来的service集合
     */
    public static void startOtherService(Context context, ArrayList<String> stopedServicesName,
            ServiceInfo[] ourServices) {
        for (int i = 0; i < stopedServicesName.size(); i++) {
            for (int j = 0; j < ourServices.length; j++) {
                ServiceInfo tempSInfo = ourServices[i];
                if (tempSInfo.name.equalsIgnoreCase(stopedServicesName.get(i).toString())) {
                    Intent intent = null;
                    try {
                        intent = new Intent(context, (Class.forName(tempSInfo.name)));
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    if (intent != null) {
                        context.startService(intent);
                    }
                }
            }
        }
    }
    
    /**
     * 关闭其他的服务(AutoTestService 这个服务不能停止)
     * 
     * @param context
     * @param ourServices 传递过来的service集合
     */
    public static ArrayList<String> stopOtherService(Context context, ServiceInfo[] ourServices,
            Class classes) {
        ArrayList<String> stopedServicesName = new ArrayList<String>();
        for (int i = 0; i < ourServices.length; i++) {
            ServiceInfo tempSInfo = ourServices[i];
            //AutoTestService 这个服务不能停止，只是一个计划服务，没有任何下载功能，不会有占带宽情况
            if(tempSInfo.name.equalsIgnoreCase(AutoTestService.class.getName()))
            {
                continue;
            }
            
            if (!tempSInfo.name.equalsIgnoreCase(classes.getName()))// 如果不是传入的类名
            {
                boolean working = NetTestUtil.serviceIsWorked(context, tempSInfo.name);// 如果服务在运行，暂停该服务
                if (working) {
                    Intent intent = null;
                    try {
                        intent = new Intent(context, (Class.forName(tempSInfo.name)));
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    if (intent != null) {
                        context.stopService(intent);
                        stopedServicesName.add(tempSInfo.name);
                    }
                }
            }
        }
        return stopedServicesName;
    }

    /**
     * 获取所有轮的测试信息
     * 
     * @param context
     * @param isNeedDelete 是否需要返回已经删除的信息，false只返回没删除的，true返回全量
     * @return ArrayList<NetTestBatchInfo> 如果为null，没有轮测试信息。
     */
    public static ArrayList<HttpTestRoundInfoBean> getHttpTestRoundsInfos(Context context,
            boolean isNeedDelete) {
        HttpTestDBControl dbControl;
        dbControl = new HttpTestDBControl(context);
        ArrayList<HttpTestRoundInfoBean> result = new ArrayList<HttpTestRoundInfoBean>();
        result = dbControl.queryAllRoundInfo(isNeedDelete);
        return result;
    }
    
    /**
     * 删除round表某条记录
     * 
     * @param context
     * @param roundInfoBean
     */
    public static void deleteRoundInfo(Context context, int _roundID) {
        HttpTestDBControl dbControl = new HttpTestDBControl(context);
        dbControl.deleteRoundInfo(_roundID);
    }
    
    /**
     * 根据startTime获取轮的测试信息
     * 
     * @param context
     * @param startTime 轮的startTime.
     * @return NetTestRoundInfoBean 如果为null，没有轮测试信息。
     */
    public static HttpTestRoundInfoBean getHttpTestRoundsInfosByStartTime(Context context,
            String startTime) {
        try {
            HttpTestDBControl dbControl;
            dbControl = new HttpTestDBControl(context);
            HttpTestRoundInfoBean result = dbControl.queryRoundInfoByStartTime(startTime);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}